<?php
namespace app\shop\model;
/*
 *表名
 */
use app\shop\controller\Wxdel;
use think\Db;

class OrderModel extends BaseModel
{
    protected $table = 'xst_order';
    protected $rule = [
        'id' => '',
    ];
    protected $msg = [
        'id' => '',
    ];
    public function getCondition($condition){
        $keywords = input('keywords');
        $state = input('ostate');
        if($state!=''){
            $condition .= ' and (o.status = '.$state.')';
        }
        if(!empty($keywords)){
            $condition .= " and (xst_user.nickName like '%".$keywords."%' or xst_area.area like '%".$keywords."%' or o.order_number like '%".$keywords."%' or xst_goods.goods like  '%".$keywords."%')";
        }
        return $condition;
    }
    /*
     * 订单列表
     */
    public function getOrderList($condition = 1 )
    {
      $orderList = $this->alias('o')->field('o.*,u.nickname,g.shop,g.small_img,g.goods as name,a.name as area_name,a.phone,a.area,a.address')
        ->join('xst_user u','u.id=o.uid','left')
        ->join('xst_goods g','g.id=o.commodity_id','left')
        ->join('xst_area a','a.id=o.aid','left')
        ->where($condition)
        ->order('o.id desc')
        ->paginate(15,false,['query'=>request()->param()]);
        return $orderList;
    }
  /*
     * 订单列表
     */
    public function refund($condition = 1)
    {
        if(input('get.sub')){
            $keywords = input('keywords');
            if(!empty($keywords)){
                $condition .= " and (xst_user.nickName like '%".$keywords."%' or xst_area.area like '%".$keywords."%' or o.order_number like '%".$keywords."%' or xst_goods.goods like  '%".$keywords."%')";
            }
           
        }
      $orderList = $this->alias('o')->field('o.*,u.nickname,g.shop,g.small_img,a.name as area_name,a.phone,a.area,a.address')
          ->join('xst_user u','u.id=o.uid','left')
          ->join('xst_goods g','g.id=o.commodity_id','left')
          ->join('xst_area a','a.id=o.aid','left')
        ->where($condition)
        ->order('o.id desc')
        ->paginate(30,false,['query'=>request()->param()]);
      //echo $this->getLastSql();die;
        return $orderList;
    }
    /*
     * 订单编辑
     */
    public function orderEdit()
    {
        if(request()->isAjax()){
            $data = input("post.");
            $id = input('oid');
            $result = $this->where('id',$id)->update($data);
            if($result){
                $orderInfo = ['code'=>1,'msg'=>'修改成功'];
            }else{
                $orderInfo = ['code'=>0,'msg'=>'修改失败'];
            }
        }else{
            $orderInfo = $this->alias('o')->field('o.*,u.nickname,g.shop,g.small_img,a.name as area_name,a.phone,a.area,a.address')
                ->join('xst_user u','u.id=o.uid','left')
                ->join('xst_goods g','g.id=o.commodity_id','left')
                ->join('xst_area a','a.id=o.aid','left')
                ->where('o.id',input('id'))
                ->find();
        }
        return $orderInfo;
    }
  //订单发货
    public function ordersend(){
        $id = request()->post('id');
        $data = [
            'status'=>2,
            'deliver_time'=>time(),
            'express'=>request()->post('express'),
            'courier'=>request()->post('courier'),
        ];
        $sql3 = $this->where('id',$id)->update($data);
        if($sql3){
            return [
                'code'=>1,
                'msg'=>'发货成功'
            ];
        }else{
            return [
                'code'=>0,
                'msg'=>'发货失败'
            ];
        }
    }
    /*
     * 订单删除
     * */
    public function orderDel()
    {
        $id = input("id");
        if(!empty($id)){
            $result = $this->where('id','in',$id)->delete();
            if($result){
                $status = ['code'=>1,'msg'=>'删除成功'];
            }else{
                $status = ['code'=>0,'msg'=>'删除失败'];
            }
        }else{
            $status = ['code'=>0,'msg'=>'请选择要删除的项'];
        }
        return $status;
    }
    //查找请求退款的订单
    public function isrefund($condition){
          $orderList = $this->alias('o')->field('o.*,u.nickname,g.shop,g.small_img,a.name as area_name,a.phone,a.area,a.address')
              ->join('xst_user u','u.id=o.uid','left')
              ->join('xst_goods g','g.id=o.commodity_id','left')
              ->join('xst_area a','a.id=o.aid','left')
            ->where($condition)
            ->paginate(30,false,['query'=>request()->param()]);
        return $orderList;

    }
    //退款原因
    public function ordershow()
    {
        $id = input("id");
        $data = Db::name('xst_refund')->where('oid',$id)->find();
        return $data;
    }
   //修改状态
    public function status($data)
    {
        if($data['status']==1)
        {
            $date['status'] = 6;
        }else{
            $date['status'] = 5;
        }
        $res=$this->where('id',$data['id'])->update($date);
        if($res){
            $status = ['code'=>1,'msg'=>'操作成功'];
        }else{
            $status = ['code'=>0,'msg'=>'操作失败'];
        }
        return $status;
    }
}
